#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int thirdMax(vector<int>& nums) {
        using LL = long long;
        vector<LL> mmax(3, LONG_MIN);
        for (auto& e : nums)
        {
            for (int i = 0; i < 3; i++)
            {
                if (e == mmax[i])
                    break;
                else if (e > mmax[i])
                {
                    for (int j = 2; j > i; j--)
                    {
                        mmax[j] = mmax[j - 1];
                    }
                    mmax[i] = e;
                    break;
                }
            }
        }
        return mmax[2] == LONG_MIN ? mmax[0] : mmax[2];
    }
};